/*
 * Copyright (c) 2008 Openviewing
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Created: 08-Feb-2004
 */
package org.openviewing.foundation.control.exception;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** Logic exception template
 *
 *  This exception records the exception as a warning with the J2SE java.util.logging.Logger.<br/>
 *  This should be used when all callers handler the problem. As in an alternative business flow, acting as a second return value.
 */
public abstract class AbstractException extends Exception
{
    /** Message constructor.
     *
     *  This raises an exception with the given message, null cause and Warning level log.
     *  @param message <code>java.lang.String</code>
     */
    public AbstractException(final String message)
    {
        this(message, (Throwable) null);
    }

    /** Message and cause constructor.
     *
     *  This raises an exception with the given message, given cause and Warning level log.
     *  @param message <code>java.lang.String</code>
     *  @param cause <code>java.lang.Throwable</code>
     */
    public AbstractException(final String message, final Throwable cause)
    {
        super(message, cause);
        // Determine which exception was thrown.
        StackTraceElement[] trace = this.getStackTrace();
        String sourceClass = trace[0].getClassName();
        String sourceMethod = trace[0].getMethodName();
        Log log = LogFactory.getLog(sourceClass);
        
        if(log.isDebugEnabled())
        {
            log.error(String.format("During %s, %s was thrown because '%s'", sourceMethod, (cause == null ? "an exception" : cause.getClass().getCanonicalName()), message), cause);
        }
        else
        {
            log.error(String.format("During %s, %s was thrown because '%s'", sourceMethod, (cause == null ? "an exception" : cause.getClass().getCanonicalName()), message));
        }
    }
}
